package org.x4ut15m.xwebshell.explorer;

import javafx.fxml.FXMLLoader;
import org.x4ut15m.xwebshell.explorer.controller.FileManagerController;
import org.x4ut15m.xwebshell.sdk.IPlugin;
import org.x4ut15m.xwebshell.sdk.controller.AbstractShellController;
import org.x4ut15m.xwebshell.sdk.session.ISession;
import org.x4ut15m.xwebshell.sdk.type.PluginType;

import java.io.IOException;

public class FileManagerPlugin implements IPlugin {
    public PluginType pluginType = PluginType.ShellManagerTab;
    public AbstractShellController shellController;
    public FileManagerController fileManagerController;
    private ClassLoader classLoader;
    private ISession webshellSession;
    private String version = "0.0.1";

    public FileManagerPlugin(AbstractShellController shellController, ISession iSession) {
        this.shellController = shellController;
        webshellSession = iSession;
    }


    @Override
    public String getPluginName() {
        return "fileManager";
    }

    @Override
    public String getVersion() {
        return version;
    }

    @Override
    public PluginType getPluginType() {
        return pluginType;
    }

    @Override
    public void loadAllSubController() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/FileManageTab.fxml"));
        fxmlLoader.setClassLoader(classLoader);
        try {
            fxmlLoader.load();
            fileManagerController = fxmlLoader.getController();
            fileManagerController.setWebshellSession(webshellSession);
            fileManagerController.setShellController(shellController);
            fileManagerController.setWebshellItem(shellController.getWebshellItem());
            fileManagerController.setClassLoader(classLoader);
            fileManagerController.loadAllSubModule();
            fileManagerController.createEventHandlerOnComponents();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ClassLoader getClassLoader() {
        return classLoader;
    }

    @Override
    public void setClassLoader(ClassLoader classloader) {
        this.classLoader = classloader;
    }

    @Override
    public Object getReadyToLoadComponent() {
        return fileManagerController.getFileManagerTab();
    }

    @Override
    public void autoCallAfterPluginLoaded() {

    }

}
